package com.hucc.redislock;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * redis 锁工厂
 */
public class RedisLockFactory {
    private static final Map<String, RedisLock> redisLockMap = new ConcurrentHashMap<String, RedisLock>();

    public RedisLockFactory(JedisPool jedisPool) {
        RedisLockContext.setJedisPool(jedisPool);
    }

    public RedisLock getLock(String lockName) {
        if (!redisLockMap.containsKey(lockName)) {
            synchronized (lockName) {
                if (!redisLockMap.containsKey(lockName)) {
                    redisLockMap.put(lockName, new RedisReentrantLock(lockName));
                }

            }
        }

        return redisLockMap.get(lockName);
    }
}
